package com.me.servlet;

import com.me.controller.Controller;
import com.me.controller.ItemController;
import com.me.utils.IOUtility;
import com.me.utils.ServUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//吕旺平
@WebServlet("/item")
public class ItemServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		Controller controller = new ItemController(request, response);
//		User u = (User) request.getSession().getAttribute("user");
//		if (u == null) {
//			response.sendRedirect("/index.jsp");
//			return;
//		}
//
//		//不允许改题
//		if (u.isConfirm()) {
//			response.sendRedirect("/result.jsp");
//			return;
//		}
		
		
		String tempState = ServUtil.parseState(request);
		
		String state = IOUtility.getProperty(tempState);
		String page = null;
		
		if (state == null || state == ""){
			state = ServUtil.getRegState(tempState,IOUtility.getProperty("i.regex"));
		}

		
		page = controller.excute(state);
		page = page == null ? "/error.jsp" : page;
		
		
		if (page.startsWith(".")){
			response.sendRedirect(page.substring(1));
		}else{
			request.getRequestDispatcher(page).forward(request,response);
		}
		
		
	}
}



